<!-- 项目审批 -->
<template>
	<view >
		<cu-custom bgColor="bg-blue2" :isBack="true" @back="backedPage"><block slot="backText">返回</block><block slot="content">盘点明细编辑</block></cu-custom>
		<view>
			<form  @submit="onSubmit">
				<view class="cu-form-group">
					<view class="title">名称</view>
					<input v-model="item.inStockGoodsDetail.goodsName" name="input" disabled="disabled"></input>
				</view>
				<view class="cu-form-group">
					<view class="title">规格</view>
					<input v-model="item.inStockGoodsDetail.spec" name="input" disabled="disabled"></input>					
				</view>
				<view class="cu-form-group">
					<view class="title">产地</view>
					<input v-model="item.inStockGoodsDetail.maker" name="input" disabled="disabled"></input>		
				</view>
				<view class="cu-form-group">
					<view class="title">型号</view>
					<input v-model="item.inStockGoodsDetail.model" name="input" disabled="disabled"></input>
				</view>
				<view class="cu-form-group">
					<view class="title">材质</view>
					<input v-model="item.inStockGoodsDetail.materialCode" name="input" disabled="disabled"></input>					
				</view>
				<view class="cu-form-group">
					<view class="title">批次</view>
					<input v-model="item.batchNum" name="input" disabled="disabled"></input>
				</view>				
				<view class="cu-form-group">
					<view class="title xrequired">货位</view>
					<input :class="!item.goodsPosi ? 'xrwaning' : ''" v-model="item.goodsPosi" name="input" @blur="checkPosi()"></input>
				    <!-- <view class="cuIcon-form text-blue" @tap="selectPosi()">选择货位</view> -->
				</view>
				<view class="cu-form-group">
					<view class="title">堆号</view>
					<input v-model="item.inStockGoodsDetail.heapNum" name="input"></input>
				</view>		
				<view class="cu-form-group">
					<view class="title">包装类型</view>
					<input v-model="item.inStockGoodsDetail.packType" name="input"></input>
				</view>
				<view class="cu-form-group">
					<view class="title">关账数量</view>
					<input v-model="item.closeQty" name="input"></input>
				</view>
				<view class="cu-form-group">
					<view class="title">关账重量</view>
					<input v-model="item.closeWeight" name="input"></input>
				</view>
				<view class="cu-form-group">
					<view class="title">实盘数量</view>
					<input v-model="item.actualQty" name="input"></input>
				</view>
				<view class="cu-form-group">
					<view class="title">实盘重量</view>
					<input v-model="item.actualWeight" name="input"></input>
				</view>
				<view class="cu-form-group">
					<view class="title">损益数量</view>
					<input v-model="item.lossOverQty" name="input"></input>
				</view>
				<view class="cu-form-group">
					<view class="title">损益重量</view>
					<input v-model="item.lossOverWeight" name="input"></input>
				</view>
				<view class="cu-form-group">
					<view class="title">实盘人</view>
					<input v-model="item.oper" name="input" disabled="disabled"></input>
				</view>
				<!-- <view class="cu-form-group">
					<view class="title">实盘日期</view>
					<input v-model="item.dat" name="input"></input>
				</view> -->
				<view>
					<button form-type="submit" class="cu-btn block bg-blue margin-tb-sm lg" :disabled="loading" type="">
						<text :class="loading ? 'cuIcon-loading2 cuIconfont-spin' : ''"></text>保存</button>
				</view>
			</form>
		</view>
		
		<view class="cu-modal" :class="isShowGoodsPosiDialog ? 'show' : ''">
			<view class="cu-dialog" :style="{ marginTop: customBarH+'px' }">
				<view class="cu-bar bg-white justify-end">
					<view class="content">选择货位</view>
					<view class="action" @tap="hideModal">
						<text class="cuIcon-close text-red"></text>
					</view>
				</view>
				<view style="height: 350px; overflow: auto;">
					<select-goods-posi style="height: 340px;" @selected="selectedPosi"/>
				</view>
				<view class="cu-bar bg-white justify-end">
					<view class="action">
						<button class="cu-btn line-green text-green" @tap="hideModal">取消</button>
						<button class="cu-btn bg-green margin-left" @tap="hideModal">确定</button>
		
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import uniTag from '@/components/uni-tag/uni-tag.vue'
	import uniIcons from '@/components/uni-icons/uni-icons.vue'
	import uniCombox from '@/components/uni-combox/uni-combox.vue'
	import selectGoodsPosi from './selectGoodsPosi'

	import {
		mapGetters
	} from 'vuex'
	import {
		getRandomArrayElements,
		filePreview,
		formatStatus,
	} from '@/utils/index.js'
	import { formatDate } from '@/utils/datetime.js'
	export default {
		components: {
			uniTag,
			uniIcons,
			uniCombox,
			selectGoodsPosi
		},
		computed: mapGetters(['user']),
		data() {
			return {
				item: { inStockGoodsDetail: {goodsName:''}, specOptions:[], modelOptions:[], materialCodeOptions:[], makerOptions:[], weightMethod:'抄码' },
				detailId: 0,
				qtyChkId:0,
				weightMethodOptions:['抄码','过地磅','过电子磅','理算','平均件重'],
				workGroupOptions:[],
				machineOptions:[],
				goodsOptions: [],
				data: [],
				defaultProps: {
					children: 'children',
					label: 'fullName'
				},
				isShowGoodsPosiDialog: false,
				selectedPosiObj: {},
				loading: false,
				goods: null,
				customBarH:this.CustomBar+20,
				winHeight:0,
				windowWidth:0,
				padding:0
			}
		},
		onLoad(options) {
			
			//  高度自适应
			uni.getSystemInfo({
				success: res => {
					this.winHeight = (res.windowHeight-this.customBarH)*0.75
					this.windowWidth = res.windowWidth * 0.15
					this.padding = res.windowWidth / 750 * 20
				}
			})
			
			this.detailId = options.detailId
			this.qtyChkId = options.qtyChkId
			if (this.detailId >0 && this.detailId > 0) {
				this.$minApi.qtyChkDetail(this.detailId).then(res => {
					const data = res
					if (data) {
						const machine = this.item.machine
						const weightMethod =  this.item.weightMethod
						
						this.item = data
						this.item.oper = this.user.name
						this.item.actualQty = this.item.closeQty
						this.item.actualWeight = this.item.closeWeight							
						// this.item.dat = this.formatDate(new Date())
						this.item.remark = '-'
						
						if(this.item.specOptions){
						    // bindSpec
						    const specOptions = this.item.specOptions.map(b => b.propertyValue)
						    this.item.specOptions = specOptions
						}
						
						if(this.item.modelOptions){
						    // bindModel
						    const modelOptions = this.item.modelOptions.map(b => b.propertyValue)
						    this.item.modelOptions = modelOptions
						    
						    if(!detail.model&&modelOptions&&modelOptions.length>0){
						    	this.item.model=modelOptions[0]
						    }
						}
						
						if(this.item.materialCodeOptions){
						    // bindMaterialCode
						    const materialCodeOptions = this.item.materialCodeOptions.map(b => b.propertyValue)
						    this.item.materialCodeOptions = materialCodeOptions
						}
						
						if(this.item.makerOptions){
							// bindMaker
							const makerOptions = this.item.makerOptions.map(b => b.propertyValue)
							this.item.makerOptions = makerOptions
						}
					}
				}).catch(err => {
					console.log(err)
				}).finally(() => {
				})
			} else {
				uni.setNavigationBarTitle({
				title: '新增盘点明细'
				})
			}

			const params = {
				filter: '',
				sorting: '',
				pageIndex: '10000',
				pageSize: 10
			  }
			this.$minApi.getGoods(params).then(res => {
				const data = res.list
				if (data) {
					this.goods = data
					this.goodsOptions = data.map(a => a.name)
				}
			}).catch(err => {
				console.log(err)
			}).finally(() => {
				this.isLoading = false
			})

		},
		methods: {
			filePreview,
			formatStatus,
			formatDate,
			onSubmit(e) {  
				
				if(!this.item.actualQty || !this.item.actualWeight) {
					uni.showToast({
						icon: 'none',
					    title: '必填项未填写',
					    duration: 2000
					})
					return
				}
				
				this.loading = true
				const _this = this
				if (this.detailId > 0) {
					this.item.closeQty = Number(this.item.closeQty)
					this.item.closeWeight = Number(this.item.closeWeight)
					this.item.actualQty = Number(this.item.actualQty)
					this.item.actualWeight = Number(this.item.actualWeight)
					this.item.lossOverQty = Number(this.item.lossOverQty)
					this.item.lossOverWeight = Number(this.item.lossOverWeight)
					this.item.qtyChkId =  Number(this.qtyChkId)
					
					_this.$minApi.updateQtyChkDetail(this.item.id, this.item).then(res => {
						uni.showToast({
						    title: '修改成功',
						    duration: 2000
						})
						
						setTimeout(() => {
							uni.redirectTo({
								url: `/pages/todo/qtyChk/detail?id=${this.qtyChkId}&currDetailId=${this.detailId}`
							})
						}, 2000)
				
					}).catch(err => {
						uni.showToast({
							icon: 'none',
						    title: '修改失败',
						    duration: 2000
						})
					}).finally(() => {
						this.loading = false
					})
				} else {
					
					this.item.closeQty = Number(this.item.closeQty)
					this.item.closeWeight = Number(this.item.closeWeight)
					this.item.actualQty = Number(this.item.actualQty)
					this.item.actualWeight = Number(this.item.actualWeight)
					this.item.lossOverQty = Number(this.item.lossOverQty)
					this.item.lossOverWeight = Number(this.item.lossOverWeight)
					this.item.qtyChkId =  Number(this.qtyChkId)
					
					// this.item.goodsInDetailID = Number(this.item.goodsInDetailID)
					_this.$minApi.createQtyChkDetail(this.item).then(res => {
						uni.showToast({
						    title: '添加成功',
						    duration: 2000
						})
						
						setTimeout(() => {
							uni.redirectTo({
								url: `/pages/todo/qtyChk/detail?id=${this.qtyChkId}&currDetailId=${this.detailId}`
							})
						}, 2000)
					}).catch(err => {
						uni.showToast({
							icon: 'none',
						    title: '添加失败',
						    duration: 2000
						})
						console.log(err)
					}).finally(() => {
						this.loading = false
					})
				}
				
			},
			hideModal(e) {
				if (this.selectedPosiObj && this.selectedPosiObj.data) {
					
					this.item.goodsPosiID = this.selectedPosiObj.data.id
					this.item.goodsPosi = this.selectedPosiObj.data.fullName
				}
				this.isShowGoodsPosiDialog = false
			},
			checkPosi() {
				
				this.$minApi.getWareHouseFullName({ fullName: this.item.goodsPosi }).then((res) => {
					if(res){
						
					}else{
						uni.showToast({
							icon: 'none',
						    title: '货位不存在',
						    duration: 2000
						})
						this.item.goodsPosi = ''
					}
				}).catch(err => {
					console.log(err)
				}).finally(() => { })

			},
			selectPosi() {	
				this.isShowGoodsPosiDialog = true
			},
			selectedPosi(val) {
				this.selectedPosiObj = val
			},
			selectedGoods(val) {
				if (this.goods && val) {
					const goods = this.goods.find(f => f.name === val)
					
					if (goods) {
						
						this.item.goodsId = goods.id
						this.item.qtyUnit = goods.qtyUnit
						this.item.weightUnit = goods.weightUnit
						
						this.item.spec = ''
						this.item.model = ''
						this.item.materialCode = ''
						this.item.maker = ''
						
						this.bindSpec(this.item, goods.id)
						this.bindModel(this.item, goods.id)
						this.bindMaterialCode(this.item, goods.id)
						this.bindMaker(this.item, goods.id)
					}
				}
				
			},
			bindSpec(item, goodsID) {
			  this.$minApi.getGoodsPropertys({
				filter: '规格',
				goodsID: goodsID,
				pageIndex: 10000,
				pageSize: 10
			  }).then((res) => {
				const options = res.list.map(a => a.propertyValue)
				item.specOptions = options
			  }).catch(err => {
				console.log(err)
			  }).finally(() => { })
			},
			bindModel(item, goodsID) {
			  this.$minApi.getGoodsPropertys({
				filter: '型号',
				goodsID: goodsID,
				pageIndex: 10000,
				pageSize: 10
			  }).then((res) => {
				const options = res.list.map(a => a.propertyValue)
				item.modelOptions = options
			  }).catch(err => {
				console.log(err)
			  }).finally(() => { })
			},
			bindMaterialCode(item, goodsID) {
			  this.$minApi.getGoodsPropertys({
				filter: '材质',
				goodsID: goodsID,
				pageIndex: 10000,
				pageSize: 10
			  }).then((res) => {
				const options = res.list.map(a => a.propertyValue)
				item.materialCodeOptions = options
			  }).catch(err => {
				console.log(err)
			  }).finally(() => { })
			},
			bindMaker(item, goodsID) {
			  this.$minApi.getGoodsPropertys({
				filter: '产地',
				goodsID: goodsID,
				pageIndex: 10000,
				pageSize: 10
			  }).then((res) => {
				const options = res.list.map(a => a.propertyValue)
				item.makerOptions = options
			  }).catch(err => {
				console.log(err)
			  }).finally(() => { })
			},
			backedPage() {
				uni.redirectTo({
					url: `/pages/todo/qtyChk/detail?id=${this.qtyChkId}&currDetailId=${this.detailId}`
				})
			},
			bindWorkGroup(){//绑定单据 默认当日保管员做第一单的 作业班组
				const params = {
					tallyClerk: this.user.name
				  }
				this.$minApi.getGoodsInDetail(params).then(res => {
					const data = res
					
					if (data) {
						this.item.workGroup = data.workGroup
					}
				}).catch(err => {
					console.log(err)
				}).finally(() => {
				})
			}
		}
	}
</script>

<style scoped>
	.xrequired::after  {
		content: '*';
		color: red;
	}
.xrwaning{
	border: 1px solid #FF0000;
}
/deep/ .xrequiredCombox .uni-combox__label span::after  {
	content: '*';
	color: red;
}
/deep/ .xrequiredCombox .uni-combox__label text::after  {
	content: '*';
	color: red;
}
/deep/.xrwaningCombox input{
	border: 1px solid #FF0000;
}
</style>
